<?

require(dirname(__FILE__).'/_base/BaseTask.php');

class Task extends BaseTask{
    
    public $_statuses = array();
    
    public $_rstatuses = array();
    
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }
    
    public function rules() {
        return array(
            // all
            array('user_id, project_id, time_create, status, archived, critical, sort, text', 'required'),
            array('user_id, project_id, time_create, status, archived, critical, sort', 'numerical', 'integerOnly'=>true),
            array('time_create', 'default', 'setOnEmpty'=>true, 'value'=>time()),
            array('user_id', 'default', 'setOnEmpty'=>true, 'value'=>Yii::app()->user->id),
            array('archived', 'default', 'setOnEmpty'=>true, 'value'=>0),
            
            // on search
            array('id, user_id, project_id, time_create, status, archived, critical, sort, text', 'safe', 'on'=>'search'),
        );
    }
    
    public function attributeLabels() {
        return array(
            'id' => TaskModule::t('fields', 'ID'),
            'user_id' => TaskModule::t('fields', 'Пользователь'),
            'project_id' => TaskModule::t('fields', 'Проект'),
            'time_create' => TaskModule::t('fields', 'Дата создания'),
            'status' => TaskModule::t('fields', 'Статус'),
            'archived' => TaskModule::t('fields', 'В архиве'),
            'critical' => TaskModule::t('fields', 'Критическая'),
            'sort' => TaskModule::t('fields', 'Порядок'),
            'text' => TaskModule::t('fields', 'Текс'),
        );
    }
    
    public function relations() {
        return array(
            'project' => array(self::BELONGS_TO, 'TaskProject', 'project_id'),
            'author' => array(self::BELONGS_TO, 'TaskUser', 'user_id'),
            'responsible' => array(self::HAS_MANY, 'TaskResponsibleUser', 'task_id', 'with'=>'user'),
            'files' => array(self::HAS_MANY, 'TaskFile', 'task_id', 'with'=>'file'),
            'time' => array(self::HAS_MANY, 'TaskTime', 'task_id',
                'with'=>'time_user',
                'order'=>'`time`.`id` DESC'
            ),
            'comments' => array(self::HAS_MANY, 'TaskComment', 'task_id',
                'with'=>array('com_author', 'com_children'),
                'condition'=>'`comments`.`parent_id`=0',
                'order'=>'`comments`.`id` DESC'
            ),
        );
    }
    
    function GetStatuses(){
        return array(
            0 => TaskModule::t('fields', 'Новая'),
            1 => TaskModule::t('fields', 'В очереди'),
            2 => TaskModule::t('fields', 'В процессе'),
            3 => TaskModule::t('fields', 'Ожидает проверки'),
            4 => TaskModule::t('fields', 'Завершена'),
            5 => TaskModule::t('fields', 'Отложена'),
            6 => TaskModule::t('fields', 'Отменена'),
        );
    }
    function GetRstatuses(){
        return array_flip(self::GetStatuses());
    }
    
    function GetLastInProject($projectID){
        $projectID = intval($projectID);
        if($projectID < 0) throw new CException(TaskModule::t('messages', 'Не указан проект.'));
        
        $criteria = new CDbCriteria;
        $criteria->addCondition('project_id', $projectID);
        $criteria->order = 'sort DESC';
        $criteria->limit = 1;
        return self::model()->find($criteria);
    }
    
    function status($status, $notSendEmeail=false){
        $status = intval($status) > 0 ? intval($status) : $this->rstatuses[$status];
        $this->status = $status;
        $result = $this->save();
        if(!$notSendEmeail){
            //@TODO send email
        }
        return $result;
    }
    
}
